/**
 * 文 件 名:  OSSUploadTask.java
 * 版    权:  Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  baoyinbo 00501
 * 修改时间:  16/6/29
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */

package com.shuaiche.yeguan.net.oss;

import android.os.AsyncTask;
import android.util.Log;


import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * 上传任务
 *
 */
public class OSSUploadBitmapTask extends AsyncTask<String, String, Boolean> {
    /**
     * oss客户端
     */
    private OSS ossClient;
    /**
     * 上传的请求列表
     */
    private List<PutObjectRequest> requests;
    private List<OSSRequestBody> requestBodys;
    /**
     * 返回的结果列表
     */
    private List<PutObjectResult> results;
    /**
     * 上传反馈
     */
    private OSSUploadCallback callback;

    /**
     * 失败错误信息
     */
    private Exception exception;
    /**
     * 失败的请求
     */
    private PutObjectRequest failPutObjectResult;

    public OSSUploadBitmapTask(@NonNull OSS ossClient) {
        this.ossClient = ossClient;
        requests = new ArrayList<PutObjectRequest>();
        requestBodys = new ArrayList<OSSRequestBody>();
        results = new ArrayList<PutObjectResult>();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        for(OSSRequestBody item : requestBodys) {
            requests.add(item.getBitmapRequest());
        }
        for (PutObjectRequest item : requests) {
            if (item == null) {
                continue;
            }
            PutObjectResult result = didUploadItem(item);
            if (result != null) {
                results.add(result);
            } else {
                return false;
            }
        }
        return true;
    }


    @Override
    protected void onPostExecute(Boolean isSuccess) {
        this.ossClient=null;
        if (isSuccess) {
            callback.onSuccess(requests, results);
        } else {
            callback.onFailure(failPutObjectResult, exception);
        }
    }

    /***
     * 上传单个任务
     *
     * @param put 单个请求
     * @return
     */
    private PutObjectResult didUploadItem(PutObjectRequest put) {
        try {
            PutObjectResult putResult = ossClient.putObject(put);
            Log.d("PutObject", "UploadSuccess");
            Log.d("ETag", putResult.getETag());
            Log.d("RequestId", putResult.getRequestId());
            return putResult;
        } catch (ClientException e) {
            // 本地异常如网络异常等
            this.failPutObjectResult = put;
            this.exception = e;
            e.printStackTrace();
        } catch (ServiceException e) {
            // 服务异常
            this.failPutObjectResult = put;
            this.exception = e;
            Log.e("RequestId", e.getRequestId());
            Log.e("ErrorCode", e.getErrorCode());
            Log.e("HostId", e.getHostId());
            Log.e("RawMessage", e.getRawMessage());
        }
        return null;
    }

    /**
     * 添加一个任务
     * @param request
     */
    public void add(@NonNull OSSRequestBody request){
        requestBodys.add(request);
    }

    /**
     * 添加任务
     * @param requests
     */
    public void addAll(List<OSSRequestBody> requests){
        this.requestBodys.addAll(requests);
    }

    /**
     * 上传
     * @param callback
     */
    public void upload(@NonNull OSSUploadCallback callback) {
        this.callback = callback;
        execute();
    }

}
